﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.IO;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;
using System.ComponentModel;
using System.Drawing.Drawing2D;
using HospitalSystem.Core;

namespace HospitalSystem
{
    public partial class reg : System.Web.UI.Page
    {
        private RegisterLog _RL;
        // SqlConnection conn = new SqlConnection(@"Data Source=SJ-PC\SQLEXPRESS;Initial Catalog=db0015;Integrated Security=True");
        string strConnString = ConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            _RL = new RegisterLog();
            String ShowValue = (string)Session["idlogin"];
         
        }
        private void MessageBox(string msg)
        {
            Label lbl = new Label();
            lbl.Text = "<script language='javascript'>" + Environment.NewLine + "window.alert('" + msg + "')</script>";
            Page.Controls.Add(lbl);
        }
        protected void reg_Click1(object sender, EventArgs e)
        {
            _RL.insertregdata(CID.Text,LastName.Text,FirstName.Text,RadioButtonList1, RadioButtonList2,birthday.Text,Nation.Text,Race.Text,Religion.Text,Foreigner.Text,LiveHomeStatus.Text,EducationStatus.Text,LiveStreetAddress.Text,LiveStreetName.Text,Livetumbon.Text,LiveCity.Text,LiveProvince.Text,LivePostcode.Text,CensusStreetAddress.Text,CensusStreetName.Text,Censustumbon.Text,CensusCity.Text,CensusProvince.Text,CensusPostcode.Text,HomePhone.Text,Mobile.Text,RecordedStaffID.Text,LiveMoveInDate2.Text,LiveMoveOutReason,CensusMoveOutReason,LiveMoveOutDate2.Text,CensusMoveInDate2.Text,CensusMoveOutDate2.Text);
            sessionsend();
            ImageUpload_f();
            Response.Redirect("~/regfamilyok.aspx");
        }
        


         



        protected void sessionsend()
        {
            Session["LastName"] = LastName.Text;
            Session["FirstName"] = FirstName.Text;
            Session["CID"] = CID.Text;
            String CIDLabel = (string)Session["CID"];
            CIDlabel.Text = CIDLabel;
           
        }

        protected void Button3_Click(object sender, EventArgs e)
        {



        }

        protected void Button2_Click1(object sender, EventArgs e)
        {
           checkcid13();
        }








        protected void checkcid()
        {
            string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
            SqlConnection conn2 = new SqlConnection(constr);
            SqlCommand cmd22 = new SqlCommand("SELECT CID FROM [Person] where CID=@CID", conn2);


            conn2.Open();
            cmd22.Parameters.AddWithValue("@CID", CID.Text);

            SqlDataReader reader32 = cmd22.ExecuteReader();



            if (reader32.Read())
            {

                MessageBox("เลขบัตรประชาชนนี้มีการลงทะเบียนแล้วครับ  กรุณาไปหน้าค้นหาเด็กแล้ว");
                CIDST.Text = "Fail!!";
                reg_button.Visible = false;
                sessionsend();

            }
            else
            {
                
                MessageBox("เลขบัตรประชาชนนี้สามารถใช้ได้");
                CIDST.Text = "Pass!!";
                reg_button.Visible = true;
                sessionsend();
            }
            conn2.Close();
            reader32.Close();
        }






        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            //สร้างที่อยู่อัตโนมัติกรณีใช้ตามทะเบียนบ้าน
            bindtextbox();
        }



        protected void bindtextbox()
        {
            Session["StreetAddress"] = CensusStreetAddress.Text;
            Session["StreetName"] = CensusStreetName.Text;
            Session["tumbon"] = Censustumbon.Text;
            Session["City"] = CensusCity.Text;
            Session["Province"] = CensusProvince.Text;
            Session["Postcode"] = CensusPostcode.Text;

            Session["inhome"] = CensusMoveInDate2.Text;
            Session["out"] = CensusMoveOutDate2.Text;
            

            String StreetAddress = (string)Session["StreetAddress"];
            LiveStreetAddress.Text = StreetAddress;
            String StreetName = (string)Session["StreetName"];
            LiveStreetName.Text = StreetName;

            String Province = (string)Session["Province"];
            LiveProvince.Text = Province;

            String Postcode = (string)Session["Postcode"];
            LivePostcode.Text = Postcode;

            String City = (string)Session["City"];
            LiveCity.Text = City;

            String tumbon = (string)Session["tumbon"];
            Livetumbon.Text = tumbon;

            String inhome = (string)Session["inhome"];
            LiveMoveInDate2.Text = inhome;
            String outhome = (string)Session["out"];
            LiveMoveOutDate2.Text = outhome;
            
        }


        //private Boolean PIDChecker(String PID)
        //{
        //    if (PID.Trim().Length != 13)
        //        return false;
        //    int y = 0;
        //    for (int i = 0; i < PID.Length - 1; i++)
        //        y += int.Parse(PID[i].ToString()) * (13 - i);
        //    int x = 11 - (y % 11);
        //    return PID[12].ToString() == x.ToString();
        //}

        protected void CID_TextChanged(object sender, EventArgs e)
        {
            checkcid13();
        }


        protected void checkcid13()
        {
            if (_RL.ValidateNumber(CID.Text))
            {
                if (_RL.PIDChecker(CID.Text))
                {
                    CIDST.Text = "Pass!!";
                    checkcid();
                }
                else
                {
                    CIDST.Text = "Fail!!";
                    MessageBox("เลขบัตรประชาชนไม่ถูกต้อง");
                    reg_button.Visible = false;
                }
            }
            else
            {
                CIDST.Text = "Fail!!";
                MessageBox("กรุณาใส่ตัวเลข");
                reg_button.Visible = false;
            }
        }




        protected void ImageUpload_f()
        {
            SqlConnection conn = new SqlConnection(strConnString);
            if (FileUploader.HasFile)
            {
                //try
                //{



                conn.Open();
                SqlCommand cmd = new SqlCommand("delete from ImageDek where Iddek=@CID", conn);
                cmd.Parameters.AddWithValue("@CID", CIDlabel.Text);
                cmd.ExecuteNonQuery();

                conn.Close();





                //ไปใส่พาธไฟล์เอา

                string MapPath = ("~/pic/profile/");
                string filename = CIDlabel.Text;
                FileUploader.SaveAs(Server.MapPath(MapPath) + filename + Path.GetExtension(FileUploader.FileName));
                //FileUploader.SaveAs(filename + ".Jpeg");
                // string SQL = "INSERT INTO [ImageDek] ([Iddek],[FilePath]) VALUES (@Iddek,@Filepath)";
                //เพิ่มไฟล์ไทป์ด้วยนะคะพี่รงคะ
                string Includepath = ((MapPath) + filename + Path.GetExtension(FileUploader.FileName));
                Bitmap originalBMP = new Bitmap(FileUploader.FileContent);
                // ใส่ขนาดที่นี่
                int newWidth = 200;
                int newHeight = 200;
                Bitmap newBMP = new Bitmap(originalBMP, newWidth, newHeight);
                Graphics oGraphics = Graphics.FromImage(newBMP);
                oGraphics.SmoothingMode = SmoothingMode.AntiAlias; oGraphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                oGraphics.DrawImage(originalBMP, 0, 0, newWidth, newHeight);
                newBMP.Save(Server.MapPath(MapPath + filename + Path.GetExtension(FileUploader.FileName)));
                originalBMP.Dispose();
                newBMP.Dispose();
                oGraphics.Dispose();

                string constr = WebConfigurationManager.ConnectionStrings["strconnection"].ConnectionString;
                SqlConnection scon = new SqlConnection(constr);
                SqlCommand cmd22 = new SqlCommand("INSERT INTO [ImageDek] ([Iddek],[FilePath]) VALUES (@Iddek,@Filepath)", scon);
                cmd22.Parameters.AddWithValue("@Iddek", CIDlabel.Text);
                cmd22.Parameters.AddWithValue("@FilePath", Includepath);



                scon.Open();
                cmd22.ExecuteNonQuery();
                scon.Close();

             
                //xxx คิวรีมาจาก db เอา
                //Image1.ImageUrl = Server.MapPath("xxx");




                //}
                //catch
                //{

                //}
            }
        }
        protected void Button3_Click1(object sender, EventArgs e)
        {

        }

        protected void ImageUpload_Click(object sender, EventArgs e)
        {
            ImageUpload_f();
        }

        protected void BDIB_Click(object sender, ImageClickEventArgs e)
        {
            BDCal.Visible = true;
        }

        protected void CMIDIB_Click(object sender, ImageClickEventArgs e)
        {
            CMIDCal.Visible = true;
        }

        protected void CMODIB_Click(object sender, ImageClickEventArgs e)
        {
            CMODCal.Visible = true;
        }

        protected void LMIDIB_Click(object sender, ImageClickEventArgs e)
        {
            LMIDCal.Visible = true;
        }

        protected void LMODIB_Click(object sender, ImageClickEventArgs e)
        {
            LMODCal.Visible = true;
        }

        protected void LMIDCal_SelectionChanged(object sender, EventArgs e)
        {
            LiveMoveInDate2.Text = LMIDCal.SelectedDate.ToShortDateString();
            LMIDCal.Visible = false;
        }

        protected void BDCal_SelectionChanged(object sender, EventArgs e)
        {
            birthday.Text = BDCal.SelectedDate.ToShortDateString();
            BDCal.Visible = false;
        }

        protected void CMIDCal_SelectionChanged(object sender, EventArgs e)
        {
            CensusMoveInDate2.Text = CMIDCal.SelectedDate.ToShortDateString();
            CMIDCal.Visible = false;
        }

        protected void CMODCal_SelectionChanged(object sender, EventArgs e)
        {
            CensusMoveOutDate2.Text = CMODCal.SelectedDate.ToShortDateString();
            CMODCal.Visible = false;
        }

        protected void LMODCal_SelectionChanged(object sender, EventArgs e)
        {
            LiveMoveOutDate2.Text = LMODCal.SelectedDate.ToShortDateString();
            LMODCal.Visible = false;
        }




    }
}